package com.adjy.pathbuilders.world;

import static com.adjy.pathbuilders.commons.Preconditions.check;
import static com.adjy.pathbuilders.commons.Preconditions.checkNotNull;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.view.MotionEvent;

import com.adjy.pathbuilders.commons.FontLoader;
import com.adjy.pathbuilders.commons.FontLoader.Fonts;
import com.adjy.pathbuilders.objects.button.Button;
import com.adjy.pathbuilders.objects.button.TextImageButton;
import com.adjy.pathbuilders.objects.button.listeners.NOPButtonListener;
import com.adjy.pathbuilders.rendering.GameCanvas;
import com.adjy.pathbuilders.rendering.loading.AnimationFactory;
import com.adjy.pathbuilders.rendering.loading.WeakReferenceBitmap;
import com.adjy.pathbuilders.screens.GameScreen;

/**
 * 
 * A window to display a warning message. It disappears after the given amount of time.
 *
 */
public class WarningWindow implements GameScreen {
	
	private final float showTime;
	private float remainingTime;
	private Button button;
	
	private int windowX;
	private int windowY;
	private AnimationFactory factory;
	private String message;
	
	public WarningWindow(AnimationFactory factory, String message, int windowX, int windowY, float showTime) {
		checkNotNull(message, "message must not be null.");
		check(windowX >= 0, "windowX must not be negative.");
		check(windowY >= 0, "windowY must not be negative.");
		check(showTime >= 0, "showTime must not be negative.");
		
		this.showTime = showTime; 
		this.windowX = windowX;
		this.windowY = windowY;
		this.factory = factory;
		this.message = message;
		
		button = new TextImageButton(windowX, windowY,
				new NOPButtonListener(),
				new WeakReferenceBitmap(factory, "menu_button_selected"),
						new WeakReferenceBitmap(factory, "menu_button_unselected"),
						message, textPaint(), strokePaint());
	}

	private void createButtons() {
		button = new TextImageButton(windowX, windowY,
				new NOPButtonListener(),
				new WeakReferenceBitmap(factory, "menu_button_selected"),
						new WeakReferenceBitmap(factory, "menu_button_unselected"),
						message, textPaint(), strokePaint());
	}
	
	private Paint textPaint() {
		Paint textPaint = new Paint();
		textPaint.setARGB(255, 255, 98, 61);
		textPaint.setAntiAlias(true);
		textPaint.setTextSize(15);
		textPaint.setTypeface(FontLoader.getInstance().getFont(Fonts.HOBO));
		return textPaint;
	}
	
	private Paint strokePaint() {
		Paint strokePaint = new Paint();
		strokePaint.setARGB(255, 172, 51, 22);
		strokePaint.setAntiAlias(true);
		strokePaint.setStrokeWidth(1);
		strokePaint.setTextSize(15);
		strokePaint.setStyle(Style.STROKE);
		strokePaint.setTypeface(FontLoader.getInstance().getFont(Fonts.HOBO));
		return strokePaint;
	}
	
	@Override
	public void load() {
		factory.loadImages("menu_button_selected", "menu_button_unselected");
		createButtons();
	}

	@Override
	public void release() {
		factory.releaseImages("menu_button_selected", "menu_button_unselected");
	}

	@Override
	public void input(MotionEvent event) {
		// TODO Auto-generated method stub

	}

	@Override
	public void render(GameCanvas mcanvas, float timeElapsed) {
		if (remainingTime > 0) {
			button.render(mcanvas, timeElapsed);
		}
		remainingTime = Math.max(0, remainingTime - timeElapsed);
	}

	@Override
	public void step(float timePassed) {
		// TODO Auto-generated method stub

	}
	
	public void show() {
		remainingTime = showTime;
	}
}
